<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace ProjetA\GroupeBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

use ProjetA\GroupeBundle\Entity\Groupe as Groupe;
use ProjetA\GroupeBundle\Entity\Page;
use ProjetA\GroupeBundle\Entity\Text;
use ProjetA\GroupeBundle\Entity\Tag;
use FOS\UserBundle\Entity\UserManager as UserInterface;
/**
 * Description of GroupeHandler
 *
 * @author Gael
 */
class GroupeEditHandler {
    protected $form;
    protected $request;
    protected $em;
    protected $groupe;
    
    public function __construct(Form $form, Request $request, EntityManager $em,Groupe $groupe)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
        $this->groupe = $groupe;
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
            $this->form->bindRequest($this->request);

            if( $this->form->isValid() )
            {
                $data= $this->request;
                
                 return $this->onSuccess($data);
                 //return true;
            }
        }

        return false;
    }

 
    
    
    
    public function onSuccess($data)
    {
        
        $this->groupe->setDescription($this->form->get('description')->getData());
        $this->groupe->setNom($this->form->get('nom')->getData());
        $this->groupe->setPublic($this->form->get('public')->getData());
        
        foreach ($this->form->get('tags')->getData() as $tag) {
             $this->em->persist($tag); 
            
        }
        $datapage =$this->form->get('page')->getData();
        
//        echo "<pre>" . print_r($datapage, true) . "</pre>";
//        $datapage =$this->form->get('pagde')->getData();
        foreach($this->groupe->getTexts() as  $text)
        {
            
            $text->setText($datapage[$text->getName()][$text->getName()]);
            $this->em->persist($text); 
            
        }
        foreach($this->groupe->getDates() as  $date)
        {
            
            $date->setDate($datapage[$date->getName()][$date->getName()]);
            $this->em->persist($date); 
            
        }
        
             
        $this->em->persist($this->groupe);
        $this->em->flush();
        return $this->groupe->getId();
        
    }
}

?>
